package com.dingding.order.core.march.biz.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.history.dto.TakeMileageDTO;
import com.dingding.order.core.march.biz.IMarchOrderService;
import com.dingding.order.dto.history.ResHistoryOrder;
import com.dingding.order.dto.march.underway.ReqHistoryOrder;
import com.dingding.order.dto.march.underway.ResUnderWayOrder;
import com.dingding.order.dto.march.underway.UderWayOrderDto;
import com.dingding.order.processer.ProcesserFactory;

/**
 * @Description:进行中订单实现
 * @author:fengshuonan
 * @date: 2016年8月19日13:25:31
 */
@SuppressWarnings("all")
@Component("driverUnderWayOrderService")
public class DriverUnderWayOrderServiceImpl extends AbstractResponse<ResUnderWayOrder, ReqHistoryOrder>
		implements IMarchOrderService {

	@Resource(name = "processerFactory")
	ProcesserFactory processerFactory;
	
	@Resource(name = "specialHistoryOrderDao")
	private IHistoryOrderDao historyOrderDao;
	
	@Override
	@Transactional(readOnly = true)
	public String getMarchOrder(String requestDate) {

		ResponseEntity<ResUnderWayOrder> responseEntity = new ResponseEntity<ResUnderWayOrder>();
		ReqHistoryOrder request = super.parseRequestJson(requestDate);
		List<UderWayOrderDto> underWayOrders = new ArrayList<UderWayOrderDto>();
		
		//验证参数
		if(!super.validateParam(responseEntity, request)){
			return super.responseBeanToJson(responseEntity);
		}
		
		//获取所有进行中订单(专车的)
		underWayOrders = processerFactory.getOrderProcesser(PublishCarType.SPECIAL).getUnderWayOrders(request.getUserId(),request.getClientType());
		
		// 获取接乘里程
		TakeMileageDTO takeMileageDTO = null;
		
		if(request.getUserRole() == 2){
			takeMileageDTO = historyOrderDao.takeMileage(request.getUserId());
		}else if(request.getUserRole() == 3){
			takeMileageDTO = historyOrderDao.taxiTakeMileage(request.getUserId());
		}
		
		ResUnderWayOrder res = new ResUnderWayOrder();
		res.setOrderList(underWayOrders);
		
		// 设置接乘里程
		if(takeMileageDTO != null && takeMileageDTO.getPickUpMeters() != null){
			res.setPickUpMeters(takeMileageDTO.getPickUpMeters());
		}else{
			res.setPickUpMeters(new BigDecimal(0));
		}
		
		res.setOrderList(underWayOrders);
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(res);
		return super.responseBeanToJson(responseEntity);
	}

	
}
